<?php
App::uses('AppController', 'Controller');
/**
 * Cars Controller
 *
 * @property Car $Car
 */
class CarsController extends AppController {

	public $components = array('Paginator');

	public function beforeFilter() {
		parent::beforeFilter();
	
		//Grant access to all the methods in this controller
		//$this->Auth->allow('servicesHistory');
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Car->recursive = 0;
		$this->set('cars', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Car->id = $id;
		if (!$this->Car->exists()) {
			throw new NotFoundException(__('Invalid car'));
		}
		$this->set('car', $this->Car->read(null, $id));
	}



/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function servicesHistory($id = null) {
		$this->Car->id = $id;
		if (!$this->Car->exists()) {
			throw new NotFoundException(__('Invalid car'));
		}
		$this->Car->recursive = 0;
		$this->set('car', $this->Car->read(null, $id));
		$this->set('carData',$this->Car->Service->find('all',array(
														'conditions'=>array('Service.car_id' => $id),
														'fields' => array('Service.fecha','Service.descripcion','Service.kilometraje'),
														'order'=> array('Service.fecha' => 'desc'),
														)));
	}


/**
 * add method
 *
 * @return void
 */
	public function add($idClient = null) {
		if ($this->request->is('post')) {
			$this->Car->create();
			if ($this->Car->save($this->request->data)) {
				$this->Session->setFlash(__('Vehículo agregado!'), 'default', array(), 'good');
				$this->redirect(array('controller' => 'clients', 'action' => 'view',$this->request->data['Car']['client_id']));
			} else {
				$this->Session->setFlash(__('Vehículo no ha sido agregado. Intente de nuevo.'), 'default', array(), 'bad');
			}
		}
		$clients = $this->Car->Client->find('list');
		$this->set(compact('clients'));
		if($idClient)
			$this->set('idClient',$idClient);
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Car->id = $id;
		if (!$this->Car->exists()) {
			throw new NotFoundException(__('Invalid car'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Car->save($this->request->data)) {
				$this->Session->setFlash(__('Datos modificados correctamente!'), 'default', array(), 'good');
				$this->redirect(array('controller'=>'clients','action' => 'view',$this->request->data['Car']['client_id']));
			} else {
				$this->Session->setFlash(__('Datos no guardados, favor intente de nuevo'), 'default', array(), 'bad');
			}
		} else {
			$this->request->data = $this->Car->read(null, $id);
		}
		$clients = $this->Car->Client->find('list',array('order'=>'Client.nombre asc'));
		$this->set(compact('clients'));
	}

/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Car->id = $id;
		if (!$this->Car->exists()) {
			throw new NotFoundException(__('Invalid car'));
		}
		if ($this->Car->delete()) {
			$this->Session->setFlash(__('El vehículo ha sido borrado correctamente!'), 'default', array(), 'good');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('El Vehículo no ha sido borrado correctamente. Intente de nuevo.'), 'default', array(), 'bad');
		$this->redirect(array('action' => 'index'));
	}
	
	
	public function search() {
		
		$chapa = $this->request->query['chapa'];
		$marca = $this->request->query['marca'];

		if($chapa != ''){
				$this->Paginator->settings = array(
			        'conditions' => array('Car.chapa' => $chapa));
				$this->set('resultados',$this->Paginator->paginate('Car'));
				$this->set('campo',$chapa);
				//$this->set('resultados',$this->Client->find('all',array('conditions'=>array('cedula'=>$cedula))));
					/*$this->set('resultados',$this->Client->find('all',
								array('conditions' => array('OR'=>
									array('nombre LIKE' => '%'.$nombre.'%',
									'cedula' => $cedula)
							))));*/
		}	
		else if($marca != ''){
			$this->Paginator->settings = array('conditions' => array('Car.marca LIKE'=>'%'.$marca.'%'));
			$resultados = $this->Paginator->paginate('Car');
			$this->set('resultados',$resultados);
			$this->set('campo',$marca);
			//$this->set('resultados',$this->Client->find('all',array('conditions'=>array('nombre LIKE'=>'%'.$nombre.'%'))));
		} else {
			$this->set('resultados',null);	
		}



	} 
	
}
